<template>
  <div>
    <c-table
      :loading="loading"
      :height="height"
      :table-data="list"
      :table-column="tableColumn"
      :is-delete="false"
      :operation="!disabled"
      @on-save="onSave"
      @modify="modifyHandle"
      @focus="focusHandle"
    ></c-table>
     <issue-tracking-dialog
      v-if="issueTrackingData.show"
      :params="issueTrackingData.params"
      @close="issueTrackingCloseHandle"
    />
  </div>
</template>

<script>
import progressApi from "@/apis/overview/progress.js";
import IssueTrackingDialog from "@/components/IssueTrackingDialog.vue";
import overviewApis from "@/apis/overview/index.js";

const issueTrackingTypeMap = {
  materiel: "WLQT",
  project: "CPQT",
  projectTest: "CPSY",
  army: "JJQK",
  preCheck: "YYSQK",
  inspect: "YSQK",
  device: "SBQT",
};

export default {
  components: {IssueTrackingDialog},
  props: {
    loading: {
      type: Boolean,
      default: false
    },
    list: {
      type: Array,
      default() {
        return []
      }
    },
    height: {
      type: String,
      default: '600px'
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      tableColumn: {
        deviceCode: {
          label: "单机代号",
          columnProp: {
            align: "center",
            "min-width": "100px",
          },
        },
        deviceName: {
          label: "单机名称",
          columnProp: {
            align: "center",
            "min-width": "100px",
          },
        },
        deviceNo: {
          type: "device-no",
          label: "出厂编号",
          columnProp: {
            align: "center",
            "min-width": "160px",
          },
        },
        deviceCount : {
          label: "单机数量",
          columnProp: {
            align: "center",
            "min-width": "100px",
          },
        },
        // deviceType: {
        //   label: "单机类型",
        //   columnProp: {
        //     align: "center",
        //     "min-width": "100px",
        //     formatter: this.typeFormatter
        //   },
        // },
        materiel: {
          type: "issue-tracking",
          label: "物料齐套",
          columnProp: {
            align: "center",
            "min-width": "174px",
            "class-name": "pd-3-0",
          },
          valueMap: {
            value: "materielPlan",
            progress: "materiel",
            count: "materielNum",
          },
        },
        project: {
          type: "issue-tracking",
          label: "设备装配",
          columnProp: {
            align: "center",
            "min-width": "174px",
            "class-name": "pd-3-0",
          },
          valueMap: {
            value: "projectPlan",
            progress: "project",
            count: "projectNum",
          },
        },
        projectTest: {
          type: "issue-tracking",
          label: "产品试验",
          columnProp: {
            align: "center",
            "min-width": "174px",
            "class-name": "pd-3-0",
          },
          valueMap: {
            value: "projectTestPlan",
            progress: "projectTest",
            count: "projectTestNum",
          },
        },
        army: {
          type: "issue-tracking",
          label: "军检情况",
          columnProp: {
            align: "center",
            "min-width": "174px",
            "class-name": "pd-3-0",
          },
          valueMap: {
            value: "armyPlan",
            progress: "army",
            count: "armyNum",
          },
        },
        preCheck: {
          type: "issue-tracking",
          label: "预验收情况",
          columnProp: {
            align: "center",
            "min-width": "174px",
            "class-name": "pd-3-0",
          },
          valueMap: {
            value: "preCheckPlan",
            progress: "preCheck",
            count: "preCheckNum",
          },
        },
        inspect: {
          type: "issue-tracking",
          label: "验收情况",
          columnProp: {
            align: "center",
            "min-width": "174px",
            "class-name": "pd-3-0",
          },
          valueMap: {
            value: "inspectPlan",
            progress: "inspect",
            count: "inspectNum",
          },
        },
        device: {
          type: "issue-tracking",
          label: "设备齐套",
          columnProp: {
            align: "center",
            "min-width": "174px",
            "class-name": "pd-3-0",
          },
          valueMap: {
            value: "devicePlan",
            progress: "device",
            count: "deviceNum",
          },
        },
        focus: {
          label: "关注",
          type: "focus",
          columnProp: {
            align: "center",
          },
        },
      },
      issueTrackingData: {
        show: false,
        params: {
          billId: 0,
          type: "",
        },
      },
    }
  },
  methods: {
     typeFormatter(row) {
      return row.deviceType ? row.deviceType.message || '' : ''
    },
    onSave(row) {
      let saveData = {
        id: row.id,
        deviceNo: row.deviceNo,
        materielPlan: row.materielPlan,
        projectPlan: row.projectPlan,
        projectTestPlan: row.projectTestPlan,
        armyPlan: row.armyPlan,
        preCheckPlan: row.preCheckPlan,
        inspectPlan: row.inspectPlan,
        devicePlan: row.devicePlan,
      }

      // let splitArr = saveData.deviceNo.split('~')
      // if (splitArr.length > 2) {
      //   return this.$message.warning('格式错误，请输入如：2001或2001~2007');
      // }

      // for(let index=0; index < splitArr.length; index++) {
      //   splitArr[index] = splitArr[index].trim()
      //   if (!/^\d+$/.test(splitArr[index])) {
      //     return this.$message.warning(`【${splitArr[index]}】包含非数字`)
      //   }
      //   splitArr[index] = parseInt(splitArr[index])
      //   if(index == 1) {
      //     if (splitArr[0]> splitArr[1]) {
      //       return this.$message.warning(`格式错误，小值在前面`)
      //     }
      //   }
      // }

      // saveData.deviceNo = splitArr.length == 1 ? `${splitArr[0]}~${splitArr[0]}` : splitArr.join('~')

      progressApi
        .progressDeviceSave(saveData)
        .then(() => {
          this.$message.success("保存成功");
          this.refresh()
        });
    },
    focusHandle(row) {
      if (this.disabled) {
        return
      }
      progressApi.progressDeviceFocus({ id: row.id }).then(() => {
        this.refresh()
      });
    },
    modifyHandle(key, row, isComplete) {
      if (this.disabled) {
        return
      }
      this.cacheModifyData = {
        key,
        row
      }
      if (isComplete) {
        overviewApis
          .saveProgressRecord({
            billId: row.id,
            type: issueTrackingTypeMap[key],
            complete: 1,
          })
          .then(() => {
            // this.refresh()
            this.issueTrackingRefreshRow()
          });
      } else {
        this.issueTrackingData.show = true;
        this.issueTrackingData.params.billId = row.id;
        this.issueTrackingData.params.type = issueTrackingTypeMap[key];
      }
    },
    issueTrackingCloseHandle(refresh) {
      if (refresh) {
        // this.refresh()
        this.issueTrackingRefreshRow()
      }
      this.issueTrackingData.show = false;
    },
    refresh() {
      this.$emit('refresh')
    },
    issueTrackingRefreshRow() {
      const {key, row} = this.cacheModifyData
      const valueMap = this.tableColumn[key].valueMap
      progressApi
        .progressDeviceFindPageInfo({
          id: row.id
        })
        .then(({ data }) => {
          const { list } = data
          const newData = list[0]
          Object.assign(row, {
            [valueMap.value]: newData[valueMap.value],
            [valueMap.progress]: newData[valueMap.progress],
            [valueMap.count]: newData[valueMap.count]
          })
        })
    }
  }
}
</script>

<style>

</style>